package com.tiancheng.trade.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.exception.BusinessException;
import com.tiancheng.trade.commom.core.exception.error.ArgumentErrorInfoEnum;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.order.mapper.ReportChannelConfigMapper;
import com.tiancheng.trade.order.model.ReportChannel;
import com.tiancheng.trade.order.model.ReportChannelConfig;
import com.tiancheng.trade.order.service.ReportChannelConfigService;
import com.tiancheng.trade.order.service.ReportChannelService;
import com.tiancheng.trade.order.util.CommonUtil;
import com.tiancheng.trade.order.util.cache.ReportCache;
import com.tiancheng.trade.order.vo.admin.QueryReportChannelVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @Author: likailun
 * @Description:
 * @Date: create in 2022/12/20 10:11
 */
@Slf4j
@Service
public class ReportChannelConfigServiceImpl implements ReportChannelConfigService {
    @Resource
    private ReportChannelConfigMapper reportChannelConfigMapper;
    @Resource
    private ReportChannelService reportChannelService;
    @Resource
    private ReportCache reportCache;

    @Override
    public List<ReportChannelConfig> queryByMainAgr(String arg) {
        final Optional<List<ReportChannelConfig>> channelConfig = reportCache.getChannelConfig(arg);
        if(channelConfig.isPresent()){
            return channelConfig.get();
        }
        final List<ReportChannelConfig> reportChannelConfigs = reportChannelConfigMapper.queryByMainAgr(arg);
        if(!CollectionUtils.isEmpty(reportChannelConfigs)){
            reportCache.cacheChannelConfig(arg,reportChannelConfigs);
        }
        return reportChannelConfigs;
    }

    @Override
    public void add(ReportChannelConfig channel) {
        final QueryWrapper<ReportChannelConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("channel_id",channel.getChannelId());
        queryWrapper.eq("is_delete",false);
        final List<ReportChannelConfig> reportChannelConfigs = reportChannelConfigMapper.selectList(queryWrapper);
        BusinessErrorInfoEnum.REPORT_REPEAT_RECORD.assertIsTrue(CollectionUtils.isEmpty(reportChannelConfigs));
        // 默认启用
        channel.setState(0);
        channel.setConfigId(CommonUtil.uuid());
        // 默认不删除
        channel.setIsDelete(0);
        channel.setCreatedDt(LocalDateTime.now());
        reportChannelConfigMapper.insert(channel);
    }

    @Override
    public void update(ReportChannelConfig channel) {
        final ReportChannelConfig reportChannel = reportChannelConfigMapper.selectById(channel.getId());
        if(null==reportChannel){
            throw new BusinessException(ArgumentErrorInfoEnum.VALID_ERROR,null,"未查询到记录");
        }
        // 删除掉缓存的配置
        Set<Long> channelIds=new HashSet(){{add(channel.getChannelId());add(reportChannel.getId());}};
        final List<ReportChannel> reportChannels = reportChannelService.queryByIds(channelIds);
        if (!CollectionUtils.isEmpty(reportChannels)){
            for (ReportChannel rc : reportChannels) {
                reportCache.delChannelConfig(rc.getMainArg());
            }
        }
        reportChannel.setLastUpdDt(LocalDateTime.now());
        reportChannelConfigMapper.updateById(channel);
    }

    @Override
    public void setState(Long id, Integer state) {
        final ReportChannelConfig reportChannel = reportChannelConfigMapper.selectById(id);
        if(null==state|| state>1||null==reportChannel){
            throw new BusinessException(ArgumentErrorInfoEnum.VALID_ERROR,null,"参数异常，未查询到记录");
        }
        if(state.equals(reportChannel.getState())){
            return;
        }
        reportChannel.setState(state);
        reportChannelConfigMapper.updateById(reportChannel);
        // 删除掉缓存的配置
        Set<Long> channelIds=new HashSet(){{add(reportChannel.getId());}};
        final List<ReportChannel> reportChannels = reportChannelService.queryByIds(channelIds);
        if (!CollectionUtils.isEmpty(reportChannels)){
            for (ReportChannel rc : reportChannels) {
                reportCache.delChannelConfig(rc.getMainArg());
            }
        }
    }

    @Override
    public void delete(Long id) {
        final ReportChannelConfig reportChannel = reportChannelConfigMapper.selectById(id);
        if(null==reportChannel){
            throw new BusinessException(ArgumentErrorInfoEnum.VALID_ERROR,null,"参数异常，未查询到记录");
        }
        if(reportChannel.getIsDelete()==1){
            return;
        }
        final ReportChannelConfig config = new ReportChannelConfig();
        config.setChannelId(id);
        config.setIsDelete(1);
        reportChannelConfigMapper.updateById(config);
        // 删除掉缓存的配置
        Set<Long> channelIds=new HashSet(){{add(reportChannel.getId());}};
        final List<ReportChannel> reportChannels = reportChannelService.queryByIds(channelIds);
        if (!CollectionUtils.isEmpty(reportChannels)){
            for (ReportChannel rc : reportChannels) {
                reportCache.delChannelConfig(rc.getMainArg());
            }
        }
    }

    @Override
    public PageInfo<ReportChannelConfig> queryBankPage(QueryReportChannelVO queryReportChannelVO) {
        return null;
    }
}
